package com.ljm.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.ljm.common.CustomerBlockHandler;
import com.ljm.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class RateLimitController {
    /*
   *  出现配置报错  调用兜底 方法  testHotKey01    设置热点规则   资源===  text01    显示  ------------当前人山人海，请稍后再试！！！  o(╥﹏╥)o
   *                                            设置 流控规则    资源===  /RateLimit/text01  显示   ------------Blocked by Sentinel (flow limiting)
     *
   * */
    @GetMapping("/RateLimit/text01")
    @SentinelResource(value = "text01",blockHandler = "testHotKey01")
    public String text01(@RequestParam(value = "p1",required = false)String p1,
                             @RequestParam(value = "p2",required = false)String p2){
        return "--------------text01";
    }
    public String testHotKey01(String p1, String p2, BlockException exception){
        return "------------当前人山人海，请稍后再试！！！  o(╥﹏╥)o";
    }
    /*   出现配置报错  使用Sentinel  自定义的报错    设置 流控规则  资源==/RateLimit/text02  显示    Blocked by Sentinel (flow limiting)
    *  */
    @GetMapping("/RateLimit/text02")
    @SentinelResource(value = "text02")
    public R text02(){
        return R.error();
    }
    /*   出现配置报错  使用Sentinel  自定义的报错    设置 流控规则  资源 ==  /RateLimit/text02_1 显示    Blocked by Sentinel (flow limiting)
     *                                                热点规则    报错  没有效果
     *
     * 错误写法
     *  */
    @GetMapping("/RateLimit/text02_1")
    @SentinelResource(value = "text02_1",blockHandler = "testHotKey01")
    public R text02_1(){
        return R.error();
    }
/*  利用SentinelResource  中的 客户自定义的方法  CustomerBlockHandler 报错   资源名 == /RateLimit/text03  流控   Blocked by Sentinel (flow limiting)
*                                                                          资源名 ==  text03   报错   {"success":true,"code":2001,"message":" 当前人山人海，请稍后再试！！！  textException02  ","data":{}}
*
* */
    @GetMapping("/RateLimit/text03")
    @SentinelResource(value = "text03",blockHandlerClass = CustomerBlockHandler.class ,blockHandler="textException02")
    public R text03(){
        return R.ok().message("按url限流测试ok  text03");

    }
    /*
    * 利用SentinelResource  中的 客户自定义的方法  CustomerBlockHandler 报错   资源名 == /RateLimit/text04  流控   Blocked by Sentinel (flow limiting)
     *                                                                      资源名 ==  text04  流控规则    报错  error 界面
     *                                                                     资源名 ==  text04   热点规则    报错   error 界面
     *                                                                      资源名 ==  text04   热点规则 无反应
     * */
    @GetMapping("/RateLimit/text04")
    @SentinelResource(value = "text04",blockHandlerClass = CustomerBlockHandler.class ,blockHandler="textException02")
    public R text04(@RequestParam(value = "p1",required = false)String p1,
                    @RequestParam(value = "p2",required = false)String p2){
        return R.ok().message("按url限流测试ok  text04");

    }


}
